package be.kuleuven.cs.mop.app.controllers;


import static junit.framework.Assert.*;

import java.util.HashMap;
import java.util.HashSet;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.app.controllers.ProjectController;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.Project;
import be.kuleuven.cs.mop.domain.model.Task;
import be.kuleuven.cs.mop.domain.model.impl.Field;
import be.kuleuven.cs.mop.domain.model.impl.Schedule;



public class ProjectControllerTest {
	
	private static TestEnvironment ENV;
	
	private static ProjectController	ctrl;
	private static Project				project;
	private static Task					task;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV = TestEnvironment.newInstance();
		
		ctrl = ENV.controllers.getControllerProjects();
		
		project = ctrl.createProject("Project");
		
		task = ENV.controllers.getControllerTasks().createTask(
				new Schedule(ENV.datum1, ENV.datum2, ENV.duration),
				new HashSet<Task>(),
				new HashMap<String, Field>(),
				ENV.testTaskType);
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	@Test(expected=TaskManagerException.class)
	public void testCreateProjectInvalidNull()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.createProject(null);
	}
	
	@Test(expected=TaskManagerException.class)
	public void testCreateProjectInvalidEmpty()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.createProject("");
	}
	
	@Test
	public void testCreateProjectValid()
			throws InvalidCurrentUserException, TaskManagerException {
		final Project p = ctrl.createProject("RandomProject");
		assertNotNull(p);
		assertTrue(ctrl.getProjects().contains(p));
	}
	
	@Test(expected=TaskManagerException.class)
	public void testAssignTaskToProjectInvalidNullA()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.assignTaskToProject(null, project);
	}
	
	@Test(expected=NullPointerException.class)
	public void testAssignTaskToProjectInvalidNullB()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.assignTaskToProject(task, null);
	}
	
	@Test
	public void testAssignTaskToProjectValid()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.assignTaskToProject(task, project);
		assertEquals(task.getProject(), project);
		assertTrue(project.getTasks().contains(task));
	}
	
	@Test(expected=NullPointerException.class)
	public void testRemoveProjectInvalidNull()
			throws InvalidCurrentUserException, TaskManagerException {
		ctrl.removeProject(null);
	}
	
	@Test
	public void testRemoveProjectValid()
			throws InvalidCurrentUserException {
		assertTrue(ctrl.getProjects().contains(project));
		try {
			ctrl.removeProject(project);
		} catch (final TaskManagerException e) {
			e.printStackTrace();
			assertFalse(true);
		}
		assertFalse(ctrl.getProjects().contains(project));
	}
	
}